package com.project.controller.web;

import com.project.myBeans.ConfigParameters;
import com.project.myBeans.ResponseBean;
import com.project.service.base.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * Created by WheelChen on 2017/5/16.
 */
@Controller
public class PublicController extends BaseService {

    @Autowired
    private ConfigParameters configParameters;


    /**
     * @api {POST} /fileUpload 文件上传
     * @apiVersion 1.0.0
     * @apiGroup Public
     *
     * @apiParam {fileUp} file 上传文件
     *
     * @apiSuccess {bool} success 返回成功
     * @apiSuccess {String} message 提示
     *
     * @apiSuccessExample Success-Response:
     * HTTP/1.1 200 OK
     * {
     *      "success": true;
     *      "message": "上传成功";
     * }
     */
    //===Web端===
    @RequestMapping(value = "/fileUpload", method = RequestMethod.POST) //tao:照片上传
    @ResponseBody
    public ResponseBean upload(@RequestParam("fileUp") CommonsMultipartFile file, HttpServletRequest request, HttpServletResponse response) throws Exception {
        //System.out.println("File!!++++++++++");
        ResponseBean responseBean = new ResponseBean();
        try {
            String savePath = request.getSession().getServletContext().getRealPath("/WEB-INF/static/upload");
            //多级地创建目录
            File f1 = new File(savePath);
            if (!f1.exists()) f1.mkdirs();


            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();


//            上传文件在服务器的文件名
            String fileName = "";


            file.getFileItem().getName();
//            for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
//                MultipartFile file = entity.getValue();
//                if (file.isEmpty()) continue;
//        }

//            获取文件格式
            String name = file.getOriginalFilename();
            //System.out.println("File:"+name);
            String ext = name.substring(name.lastIndexOf('.'));

//            保存到服务器后的新文件名
            fileName = new Date().getTime() + ext;

//            创建文件保存对象
            File f = new File(savePath + "/" + fileName);

//            获取文件输出流
            FileOutputStream out = new FileOutputStream(f);

//            保存文件
            FileCopyUtils.copy(file.getInputStream(), out);

            System.out.println(fileName);

            responseBean.setSuccess(true);
            responseBean.setData(fileName);

            return responseBean;
        } catch (IOException e) {
            e.printStackTrace();
            responseBean.setSuccess(false);
            return responseBean;
        }
    }
}
